package com.example.springbootrediscache.controller;

import com.example.springbootrediscache.entity.User;
import com.example.springbootrediscache.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    @Qualifier(value = "userServiceImpl")
    private UserServiceImpl userService;

    // 添加用户
    @RequestMapping("/add/{id}/{username}")
    public User insertUser(@PathVariable Long id, @PathVariable String username) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword("xxx");
        userService.insertUser(user);
        return user;
    }

    // 查找用户
    @RequestMapping("/query/{id}")
    public User findUserById(@PathVariable Long id) {
        return userService.findUserById(id);
    }

    // 修改用户
    @RequestMapping("/update/{id}/{userName}")
    public User updateUserById(@PathVariable Long id, @PathVariable String userName) {
        User user = new User();
        user.setId(id);
        user.setUsername(userName);
        user.setPassword("xxx");
        return userService.updateUserById(user);
    }

    // 删除用户
    @RequestMapping("/remove/{id}")
    public Boolean deleteUserById(@PathVariable Long id) {
        userService.deleteUserById(id);
        return true;
    }

}
